using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public class GameScenceChange : MonoBehaviour
{
    public string path;
    private GameObject nextScence;
    public bool isActive = true;
    private GameObject NextScence
    {
        get {
            if (nextScence == null)
            {
                Debug.Log(path);
                nextScence = Instantiate(Resources.Load<GameObject>(path));
            }
            return nextScence;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (isActive)
        {
            if (collision.gameObject.tag == "Player")
            {
                StartCoroutine("IChangeScence", collision);
            }
        }
    }

    private IEnumerator IChangeScence(Collider2D collision)
    {
        UIManager.instance.ShowWaitLoadUI();
        yield return new WaitForSeconds(0.5f);
        collision.transform.position = NextScence.GetComponent<InGameScence>().enterPosition.position;
        yield return new WaitForSeconds(0.5f);
        UIManager.instance.UnShowWaitLoadUI();
        isActive = false;
    }

}
